<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
//use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use HuangYi\Rbac\RbacTrait;

class User extends Authenticatable
{
    use  Notifiable;
//    use ValidatingTrait;
    use SoftDeletes;
    use RbacTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','token_api',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $rules = [
        'name' => 'required|max:20|unique:users',
        'email' => 'required|email|unique:users',
    ];

    public function info()
    {
        return $this->hasOne('App\Models\UserInfo','u_id');
    }
    protected $validationMessages = [
        'name.required' => '用户名必填',
        'name.max' => '用户名长度不得超过20个字符',
        'name.unique' => '该用户名已经被注册过了',
        'email.required' => '邮箱必填',
        'email.email' => '无效的邮箱格式',
        'email.unique' => '该邮箱已被注册',
    ];
    protected $appends = ['role','wechat'];
    public function getRoleAttribute()
    {
        $p = \DB::table('user_role')
            ->where(['user_id'=>$this->attributes['id']])->first();
        $result='';
        if($p){
            $result = $p->role_id;
        }
        return $result;
    }
    public function getWechatAttribute()
    {
        $p = UserWechat::query()
            ->where(['user_id'=>$this->attributes['id']])->get();
        $result=[];
        if(!empty($p)){
            foreach ($p as $item){
                $result[]= $item->wechat_id;
            }
        }
        return $result;
    }


}
